﻿using System;
using System.Collections.Generic;
using System.Text;

class ConvertFromAnySystemToAnySystem
{
    static void Main()
    {
        //Exercise 7.
        //Write a program to convert from any numeral system of given base s to any other numeral system of base d (2 ≤ s, d ≤  16).

        //For example: 11111110 -> FE -> 254;  000111110100 -> 1F4 -> 500

        Console.Write("Enter your number: ");
        string number = Console.ReadLine();
        Console.Write("Convert from a system of base s: ");
        int s = int.Parse(Console.ReadLine());
        Console.Write("Convert to a system of base d: ");
        int d = int.Parse(Console.ReadLine());
        if (s == 16)
        {
            char[] decArray = number.ToCharArray();
            Array.Reverse(decArray);
            int decimalResult = 0;
            for (int i = 0; i < decArray.Length; i++)
            {
                int digit = 0;
                switch (decArray[i])
                {
                    case 'A': digit = 10;
                        break;
                    case 'B': digit = 11;
                        break;
                    case 'C': digit = 12;
                        break;
                    case 'D': digit = 13;
                        break;
                    case 'E': digit = 14;
                        break;
                    case 'F': digit = 15;
                        break;
                    default: digit = int.Parse(decArray[i].ToString());
                        break;
                }
                for (int j = 0; j < i; j++)
                {
                    digit = digit * 16;
                }

                decimalResult += digit;
            }

            List<string> final = new List<string>();
            while (decimalResult > 0)
            {
                switch (decimalResult % d)
                {
                    case 10:
                        final.Add("A");
                        break;
                    case 11:
                        final.Add("B");
                        break;
                    case 12:
                        final.Add("C");
                        break;
                    case 13:
                        final.Add("D");
                        break;
                    case 14:
                        final.Add("E");
                        break;
                    case 15:
                        final.Add("F");
                        break;
                    default:
                        final.Add((decimalResult % d).ToString());
                        break;
                }
                decimalResult = decimalResult / d;
            }
            final.Reverse();
            Console.WriteLine("This is the representation of the number with base {0}:", d);
            for (int i = 0; i < final.Count; i++)
            {
                Console.Write(final[i]);
            }
            Console.WriteLine();

        }
        else
        {
            StringBuilder reversion = new StringBuilder();
            for (int i = number.Length - 1; i >= 0; i--)
            {
                reversion.Append(number[i]);
            }
            number = reversion.ToString();
            //Conversion from number of base s to decimal number.
            int decimalNumber = 0;
            for (int i = 0; i < number.Length; i++)
            {
                decimalNumber += int.Parse(number[i].ToString()) * Convert.ToInt32(Math.Pow(s, i));
            }
            if (d == 10)
            {
                Console.WriteLine("The converted number with base {0} is {1} .", d, decimalNumber);
            }
            else
            {

                List<string> final = new List<string>();
                while (decimalNumber > 0)
                {
                    switch (decimalNumber % d)
                    {
                        case 10:
                            final.Add("A");
                            break;
                        case 11:
                            final.Add("B");
                            break;
                        case 12:
                            final.Add("C");
                            break;
                        case 13:
                            final.Add("D");
                            break;
                        case 14:
                            final.Add("E");
                            break;
                        case 15:
                            final.Add("F");
                            break;
                        default:
                            final.Add((decimalNumber % d).ToString());
                            break;
                    }
                    decimalNumber = decimalNumber / d;
                }
                final.Reverse();
                Console.WriteLine("This is the representation of the number with base {0}:", d);
                for (int i = 0; i < final.Count; i++)
                {
                    Console.Write(final[i]);
                }
                Console.WriteLine();
            }
        }
    }
}

